/*!
 * Copyright (C) 2020 - All Rights Reserved by
 * @author : ZhaoYanbo
 * @email  : zyb920@hotmail.com
 * @created: 2020-10-31
 * @version: 1.0.0.0
 *
 */


#include "graphicsview.h"
#include "arcitem.h"
#include <QResizeEvent>
#include <cmath>
#include <QtMath>
#include <QtDebug>
#include <QDateTime>
#include <QGLWidget>
#include <QGraphicsEllipseItem>

GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent), mR(20.0), mR2(80.0)
{
    pScene = new QGraphicsScene(this);
    this->setScene(pScene);
    this->setFrameShape(QFrame::NoFrame);

    setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    QGLWidget *glw = new QGLWidget(QGLFormat( QGL::SampleBuffers ), this);
    glw->makeCurrent();
    this->setViewport(glw);
    this->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
}

void GraphicsView::init(int round, int count)
{
    mRound = round;
    mCurRound = round - 1;
    mCount = count;
    mAngle = 180.0 / count;

    qsrand(QDateTime::currentMSecsSinceEpoch());

#if 1
    if(pTimer == nullptr)
    {
        pTimer = new QTimer(this);
        pTimer->setInterval(200);
        pTimer->setSingleShot(true);
        connect(pTimer, &QTimer::timeout, this, &GraphicsView::doTimerOut);
    }
    pTimer->start();

#else
    double itemW;
    double itemH;
    double angle = 180.0 / 2 / count;
    double y;
    for(int i=round-1; i>=0;--i)
    {
        itemW = (i + 2) * mR * sin(angle * M_PI / 180) * 2;
        itemH = (i + 2) * mR - (i + 1) * mR * cos(angle * M_PI / 180);

        for(int j=0;j<count * 2;++j)
        //for(int j=0;j<2;++j)
        {
            y = (i+2) * mR - itemH/2;
            ArcItem *item = new ArcItem(this);
            item->setSize(itemW, itemH);
            item->setInfo(mR, i+1, angle * 2);
            item->setPos(0, -y);
            item->setColor(qrand() % 255, qrand() % 255, qrand() % 255, 240);
            item->setTransformOriginPoint(QPointF(0, y));
            item->setRotation(angle * 2 * j);
            item->setZValue(1);
            pScene->addItem(item);
        }
    }
#endif


}

void GraphicsView::addItem(int round)
{
    const double r1 = mR * (round + 1) + mR2;
    double y;
    double itemW = r1 * sin(mAngle / 2 * M_PI / 180) * 2;
    double itemH = r1 - (r1 - mR) * cos(mAngle / 2 * M_PI / 180);


    for(int j=0; j<mCount * 2; ++j)
    {
        y = r1 - itemH/2;
        ArcItem *item = new ArcItem(this);
        item->setInfo(r1  * 2, mAngle);
        item->setSize(itemW, itemH);
        item->setPos(0, -y);
        item->setColor(qrand() % 255, qrand() % 255, qrand() % 255, 240);
        item->setTransformOriginPoint(QPointF(0, y));
        item->setRotation(mAngle * j);
        item->setZValue(1);
        pScene->addItem(item);
    }

    //空心圆
    if(mp_circleItem == nullptr)
    {
        mp_circleItem = new QGraphicsEllipseItem;
        mp_circleItem->setPos(0,0);
        mp_circleItem->setZValue(2);
        mp_circleItem->setBrush(Qt::white);
        mp_circleItem->setPen(Qt::NoPen);
        pScene->addItem(mp_circleItem);
    }
    mp_circleItem->setRect(-(mR * round + mR2) + 1, -(mR * round + mR2) + 1,
                  (mR * round + mR2) * 2 - 2, (mR * round + mR2) * 2 - 2);
    mp_circleItem->update();
}

void GraphicsView::doTimerOut()
{
    addItem(mCurRound);
    --mCurRound;
    if(mCurRound >= 0)
        pTimer->start();
}

void GraphicsView::resizeEvent(QResizeEvent *event)
{
    QGraphicsView::resizeEvent(event);

    int w = event->size().width();
    int h = event->size().height();

    pScene->setSceneRect(-w/2,-h/2,w,h);
}
